package co.com.ticci.mb;

import java.util.ArrayList;

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;

import org.primefaces.component.datatable.DataTable;

import co.com.autolavado.ln.VehiculoLn;
import co.com.ticci.autolavado.vo.TicciVehiculo;
import co.com.ticci.autolavado.vo.TicciVehiculoPK;
import co.com.ticci.mb.general.Crud;
import co.com.ticci.mb.general.GeneralMB;

@ManagedBean(name="VehiculoMB")
@SessionScoped

public class VehiculoMB extends GeneralMB
{
	private VehiculoLn 		vehiculoLn;
	private TicciVehiculo	vehiculoSeleccionado;
	private DataTable		tableVehiculos;
	
	public VehiculoMB()
	{
		vehiculoLn = new VehiculoLn();
	}
	
	public void seleccionarEmpresa(AjaxBehaviorEvent evento)
	{
		vehiculoSeleccionado = (TicciVehiculo) tableVehiculos.getRowData();
	}

	@Override
	public void insertar(ActionEvent event) 
	{
		TicciVehiculoPK vehiculoPk 	= new TicciVehiculoPK();
		TicciVehiculo 	vehiculo 	= new TicciVehiculo();		
		vehiculoPk.setEmprCons((int)getTo().getEmprCons());
		vehiculoPk.setVehiCons(-1);
		vehiculo.setId(vehiculoPk);		
		crud.agregarElemento(vehiculo);
	}
	
	public void borrar(ActionEvent event) 
	{
		crud.eliminarElemento(vehiculoSeleccionado);
	}
	
	public void grabar(ActionEvent event) 
	{
		
		ArrayList<ArrayList<? extends Object>> resultadoCrud = crud.getResultadosCrud();
		ArrayList<TicciVehiculo> insertables                  = (ArrayList<TicciVehiculo>) resultadoCrud.get(1);
		
		if(crud.existenCambios())
		{
			int                    ultimoConsecutivo             = vehiculoLn.getUltimoVehiCons();
			for (TicciVehiculo ticciVehiculo : insertables) 
			{
				ticciVehiculo.getId().setEmprCons(ultimoConsecutivo);
				ultimoConsecutivo ++;
			}
			
			crud.guardarCambios();
		}
		
	}

	public DataTable getTableVehiculos() {
		return tableVehiculos;
	}

	public void setTableVehiculos(DataTable tableVehiculos) {
		this.tableVehiculos = tableVehiculos;
	}
	
}
